
package cn.careerx.app.common.models;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.SqlQuery;
import com.avaje.ebean.SqlRow;

import redis.clients.jedis.Jedis;

public class Assessment {
	private String id;          //测试编号
	private String title;       //测试名称
	private Integer descr;      //测试说明
	private Integer questions;  //问题JSON格式
	private Date beginAt;       //开始时间
	private Date endAt;         //结束时间
	private Integer isValid;    //是否有效：1有效；0无效
	private Date createAt;      //创建时间
	
	public static Assessment create(){
		return new Assessment();
	}
	
	public String getId() {
		return id;
	}
	public Assessment setId(String id) {
		this.id = id;
		return this;
	}
	public String getTitle() {
		return title;
	}
	public Assessment setTitle(String title) {
		this.title = title;
		return this;
	}
	public Integer getDescr() {
		return descr;
	}
	public Assessment setDescr(Integer descr) {
		this.descr = descr;
		return this;
	}
	public Integer getQuestions() {
		return questions;
	}
	public Assessment setQuestions(Integer questions) {
		this.questions = questions;
		return this;
	}
	public Date getBeginAt() {
		return beginAt;
	}
	public Assessment setBeginAt(Date beginAt) {
		this.beginAt = beginAt;
		return this;
	}
	public Date getEndAt() {
		return endAt;
	}
	public Assessment setEndAt(Date endAt) {
		this.endAt = endAt;
		return this;
	}
	public Date getCreateAt() {
		return createAt;
	}
	public Assessment setCreateAt(Date createAt) {
		this.createAt = createAt;
		return this;
	}
	
	public Integer getIsValid() {
		return isValid;
	}

	public Assessment setIsValid(Integer isValid) {
		this.isValid = isValid;
		return this;
	}

	/**setAssessmentInfoInRedis:在Redis中添加测试试题信息
	 * Author:vjudge
	 * @param db
	 * @param jedis
	 * @param asentId
	 * @return
	 */
	public static boolean setAssessmentInfoInRedis(Database db, Jedis jedis, String asentId){
		try{
			EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
			StringBuffer sqlStr = new StringBuffer();
			sqlStr.append("SELECT * FROM CX_ASSESSMENT WHERE IS_VALID=1 AND ID=?");
			SqlQuery sqlQuery = es.createSqlQuery(sqlStr.toString()).setParameter(1, asentId);
			SqlRow sqlRow = sqlQuery.findUnique();
			if( sqlRow == null ){
				return false;
			}
			Map<String, String> itemMap = new HashMap<String, String>();
			itemMap.put("id", sqlRow.getString("ID"));
			itemMap.put("title", sqlRow.getString("TITLE"));
			itemMap.put("descr", sqlRow.getString("DESCR"));
			itemMap.put("questions", sqlRow.getString("QUESTIONS"));
			itemMap.put("beginAt", sqlRow.getString("BEGIN_AT"));
			itemMap.put("endAt", sqlRow.getString("END_AT"));
			itemMap.put("isValid", sqlRow.getString("IS_VALID"));
			itemMap.put("createAt", sqlRow.getString("CREATE_AT"));
			
			jedis.hmset("assessmentInfo:"+sqlRow.getString("ID"), itemMap);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return false;
	}
	
	/**assessmentGetList：获取试卷信息
	 * Author:vjudge
	 * @param db
	 * @param jedis
	 * @param asentId
	 * @return
	 */
	public static Map<String, String> assessmentGetList(Database db, Jedis jedis, String asentId){
		Map<String, String> resultMap = new HashMap<String, String>();
		if( !jedis.exists( "assessmentInfo:" + asentId ) ){
			setAssessmentInfoInRedis(db, jedis, asentId);
		}
		resultMap = jedis.hgetAll("assessmentInfo:" + asentId );
		
		return resultMap;
	}
	
	/**calculateOccupationByAssessment:根据试卷结果计算适合职业
	 * Author:vjudge
	 * @param db
	 * @param jedis
	 * @param answerId
	 * @return
	 */
	public static ArrayList<HashMap<String, String>> calculateOccupationByAssessment(Database db, Jedis jedis, int[] answers,List< HashMap<String, String> > occupations,List< List<String> > occupRule){
		ArrayList<HashMap<String, String>> result= new ArrayList<HashMap<String, String>>();
		try {
			int[] resultCnt = new int[ occupations.size() ];
			for( int i=0; i<answers.length; i++ ){
				int ans = answers[i];
				List<String> qRule = occupRule.get(i);
				if( qRule.size() > ans ){
					String oRule = qRule.get(ans);
					for( int j=0; j<oRule.length(); j++ ){
						char  item =  oRule.charAt(j);
						int cnt = ( item=='1'? 1:0 );
						resultCnt[j] += cnt;
					}
				}
			}
			int max = 2;
			int cnt = 1;
			for( int i=2; i<occupations.size(); i++ ){
				if(resultCnt[i] > resultCnt[max]){
					max = i;
					cnt = 1;
				}
				else if (resultCnt[i] == resultCnt[max] && i!=max) {
					cnt ++;
				}
			}
			
			if ( answers[5]==2 && answers[6]==2 ) {
				Date myDate = new Date();
				long flag = myDate.getTime();
				if ((flag % 2) == 0 ) {
					result.add( occupations.get(0) );
					result.add( occupations.get(1) );
				}
				else{
					result.add( occupations.get(1) );
					result.add( occupations.get(0) );
				}
			}
			else if (answers[5]==2 || answers[6]==2){
				if (answers[5] == 2) {
					result.add( occupations.get(1) );
				}
				if (answers[6]==2) {
					result.add( occupations.get(0) );
				}
				result.add(occupations.get(max));
			}
			else if ((answers[5]==0 && answers[6]==0)||(cnt >= 2)){
				for( int i=2; i<occupations.size(); i++ ){
					if(resultCnt[i] == resultCnt[max]){
						result.add(occupations.get(i));
					}
				}
			}
			else {
				result.add(occupations.get(max));
				if (answers[5]==1 && answers[6]==1) {
					Date myDate = new Date();
					long flag = myDate.getTime();
					if ((flag % 2) == 0 ) {
						result.add( occupations.get(0) );
					}
					else{
						result.add( occupations.get(1) );
					}
				}
				else if (answers[5]==1) {
					result.add( occupations.get(1) );
				}
				else{
					result.add( occupations.get(0) );
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
}
